package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.dto.DishDto;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired(required = false)
    private DishService dishService;

    @Autowired(required = false)
    private DishFlavorService dishFlavorService;

    //保存新增菜品
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto, HttpSession session){
         //1.获取登录用户，补全信息
        Long empId = (Long) session.getAttribute("employee");
        dishDto.setCreateUser(empId);
        dishDto.setUpdateUser(empId);

        //2.把数据提交给service
        dishService.save(dishDto);
        return R.success("保存成功");
    }

    //分页查询菜品信息
    @GetMapping("/page")
    public R<Page<DishDto>> page(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize,String name){
        R<Page<DishDto>> result = dishService.findBypage(page,pageSize,name);
        return result;
    }

    //修改菜品页面数据回显
    @GetMapping("/{dishId}")
    public R<DishDto> findById(@PathVariable("dishId") Long dishId){
        R<DishDto> result = dishService.findById(dishId);
        return result;
    }

    //修改菜品
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto,HttpSession session){
        //1. 获取当前登陆者,设置当前的修改人
        Long empId = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(empId);
        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品成功");
    }

    //根据分类查询菜品
    @GetMapping("/list")
    public R<List<DishDto>> list(Long categoryId  , Integer status){
        List<DishDto> dishList = dishService.findByCategoryId(categoryId,status);
        return R.success(dishList);
    }

    //删除菜品
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        dishService.deleteById(ids);
        return R.success("删除成功");
    }

    //修改售卖状态
    @PostMapping("/status/{status}")
    public R<String> updateStatusById(@RequestParam List<Long> ids,@PathVariable Integer status){
        dishService.updateStatusById(ids , status);
        return R.success("该菜品售卖状态修改成功");
    }
}
